package cn.edu.nju.software.android.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.StreamCorruptedException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;

public class LocalFileHelper {
	public static void saveObjects(Context t, ArrayList<? extends Serializable> objects, String fileName){
		ObjectOutputStream output = null;
		try {
			t.deleteFile(fileName);
			output = new ObjectOutputStream(t.openFileOutput(fileName,0));
			output.writeObject(objects);
			output.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(output != null)
					output.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	@SuppressLint("SimpleDateFormat")
	public static String savePic(Context t, byte[] pic){
		String fileName = "";
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy_MM_dd_HH_mm");
		fileName = format.format(date);
		File file = new File(fileName);
		while(file.exists()){
			date = new Date();
			fileName = format.format(date);
			file = new File(fileName);
		}
		FileOutputStream output = null;
		try {
			output = t.openFileOutput(fileName,0);
			t.openFileOutput(fileName,0);
			output.write(pic);
			output.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(output != null)
				try {
					output.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		return t.getFilesDir() + "/" +fileName;
	}
	
	public static String savePic(Context t, InputStream is){
		String fileName = "";
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SSSS");
		fileName = format.format(date);
		BufferedInputStream bis = new BufferedInputStream(is);
		FileOutputStream output = null;
		try {
			output = t.openFileOutput(fileName,0);
			t.openFileOutput(fileName,0);
			byte [] buffer = new byte[256];
			int bytesRead = 0;
			while((bytesRead = bis.read(buffer)) != -1) {
			    output.write(buffer, 0, bytesRead);
			}
			output.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(output != null)
				try {
					is.close();
					output.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		return  fileName;
	}
	
	@SuppressWarnings("rawtypes")
	public static List loadObjectsListFromFile(Context t, String fileName){
		List returnList = null;
		if(t.getFileStreamPath(fileName).exists()){
			ObjectInputStream input = null;
			try {
				FileInputStream in = t.openFileInput(fileName);
				input = new ObjectInputStream(in);
				try {
					returnList =  (List) input.readObject();

				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
			} catch (StreamCorruptedException e) {
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				try {
					if(input != null)
						input.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return returnList;
	}
}
